package algorithmExample.tree;

import lombok.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @Author: zhangrongqing
 * @Date: 2019/11/20 9:58
 */
//题目：https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
public class MaximumDepthOfBinaryTree {
    public int maxDepth(TreeNode root) {
        int leftDepth = 0, rightDepth = 0;
        if (root.left != null) {
            leftDepth = maxDepth(root.left) + 1;
        }
        if (root.right != null) {
            rightDepth = maxDepth(root.right) + 1;
        }
        return Math.max(rightDepth, leftDepth);
    }

    //利用队列初始化二叉树，没有解决null问题和数组越界
    public static void main(String[] args) {
        List<Integer> inputList = Arrays.asList(3, 9, 20, 0, 0, 15, 7);
        TreeNode root = new TreeNode(inputList.get(0));
        TreeNode tmp;
        LinkedList<TreeNode> linkedList = new LinkedList();
        linkedList.add(root);
        int curPos = 1;
        while (curPos < inputList.size()) {
            tmp = linkedList.poll();
            linkedList.add(new TreeNode(inputList.get(curPos++)));
            tmp.setLeft(linkedList.getLast());
            linkedList.add(new TreeNode(inputList.get(curPos++)));
            tmp.setRight(linkedList.getLast());
        }
        System.out.println(root);
    }
}

